<?php
/**
 *
 * @author andrew carlson <andrew@acstudiosllc.com>
 * @version 0.0.1
 * @copyright 2012 ACStudios LLC
 * @package default
 */

namespace Encompass\Projects;
use PDO;

/**
 * Projects Model
 *
 * @package default
 * @author  andrew carlson <andrew@acstudiosllc.com>
 */
 class projectsModel {

	/**
	 * just a simple list of issues to get something going
	 */
	function view() {
		$sql = "SELECT story_id, story_type.type AS type, priority, project_state.state AS state, estimate, description, date(FROM_UNIXTIME(started_on)) as started_on
  FROM stories
  LEFT JOIN story_type ON story_type.id = stories.story_type
  LEFT JOIN project_state ON project_state.id = stories.state
  LEFT JOIN users ON users.user_id = stories.owner_id
  ORDER BY type, priority
  ";
  
  		\Encompass\Utilities\utilities::dbConnect();
		
		$query = $this->db->prepare($sql);
		
		try {
			$query->execute();
		} catch (PDOException $e) {
			throw new Exception ("PDO Error " . $e->getMessage());
		}
		
		$results = $query->fetchAll(PDO::FETCH_ASSOC);
		
		return $results;
	}
	
	function projectsByUser() {
		$url = explode("/", $_SERVER['REQUEST_URI']);
		$user_id = $url[3];

		\Encompass\Utilities\utilities::dbConnect();
		
		$sql = "SELECT story_id, type, priority, estimate, project_state.state, description FROM stories 
  			LEFT JOIN story_type ON story_type.id = stories.story_type
  			LEFT JOIN project_state ON project_state.id = stories.state
			WHERE owner_id = $user_id
			ORDER BY story_type, priority
		";
		
		// should this be in a try/catch?
		$query = $this->db->query($sql);
		
		$result = $query->fetchAll(PDO::FETCH_ASSOC);
		
		return $result;
	}
	
 }
?>
